package com.ovopark.organize.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ovopark.organize.server.mapper.FranchiseesApprovalTemplateFieldMapper;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateField;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateFieldSelect;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateFieldValue;
import com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateSection;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateFieldSelectService;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateFieldService;
import com.ovopark.organize.server.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wxb
 * @Title: FranchiseesApprovelServiceImpl
 * @Description: 加盟商立项模板字段
 * @date 2024/4/11 15:51
 **/
@Service
@Slf4j
public class FranchiseesApprovalTemplateFieldServiceImpl extends ServiceImpl<FranchiseesApprovalTemplateFieldMapper, FranchiseesApprovalTemplateField> implements FranchiseesApprovalTemplateFieldService {

    @Resource
    private FranchiseesApprovalTemplateFieldSelectService franchiseesApprovalTemplateFieldSelectService;

    /**
     * @description: 获取分组下的字段
     * @param: [sectionId, groupId]
     * @return: java.util.List<com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateField>
     * @author wxb
     * @date: 2024/6/3 14:19
     */
    @Override
    public List<FranchiseesApprovalTemplateField> getFieldListBySectionId(Integer sectionId) {
        List<FranchiseesApprovalTemplateField> list = new ArrayList<>();
        if (sectionId != null) {
            LambdaQueryWrapper<FranchiseesApprovalTemplateField> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FranchiseesApprovalTemplateField::getSectionId, sectionId);
            queryWrapper.eq(FranchiseesApprovalTemplateField::getIsDeleted, 0);
            queryWrapper.orderByAsc(FranchiseesApprovalTemplateField::getSort);
            List<FranchiseesApprovalTemplateField> fields = this.baseMapper.selectList(queryWrapper);
            if (CollectionUtil.isNotEmpty(fields)) {
                fields.forEach(field -> {
                    // 获取select
                    List<FranchiseesApprovalTemplateFieldSelect> selectList = franchiseesApprovalTemplateFieldSelectService.getSelectListByFieldId(field.getId());
                    field.setFieldSelectList(selectList);
                    list.add(field);
                });
            }
        }
        return list;
    }

//    @Override
//    public List<FranchiseesApprovalTemplateField> getFieldValueListBySectionId(Integer sectionId, Integer templateId, Integer groupId, Integer approvalId, String version) {
//        List<FranchiseesApprovalTemplateField> list = new ArrayList<>();
//        if (sectionId != null) {
//            LambdaQueryWrapper<FranchiseesApprovalTemplateField> queryWrapper = new LambdaQueryWrapper<>();
//            queryWrapper.eq(FranchiseesApprovalTemplateField::getSectionId, sectionId);
//            queryWrapper.eq(FranchiseesApprovalTemplateField::getIsDeleted, 0);
//            queryWrapper.orderByAsc(FranchiseesApprovalTemplateField::getSort);
//            List<FranchiseesApprovalTemplateField> fields = this.baseMapper.selectList(queryWrapper);
//            if (CollectionUtil.isNotEmpty(fields)) {
//                List<Integer> fieldIds = fields.stream().map(FranchiseesApprovalTemplateField::getId).collect(Collectors.toList());
//                Map<Integer, FranchiseesApprovalTemplateFieldValue> fieldValueMap = franchiseesApprovalTemplateFieldValueService.getFieldValueList(sectionId, templateId, groupId, approvalId, version, fieldIds);
//                fields.forEach(field -> {
//                    if (fieldValueMap != null) {
//                        Integer key = field.getId();
//                        FranchiseesApprovalTemplateFieldValue value = fieldValueMap.get(key);
//                        if (value != null) {
//                            field.setValueName(value.getValueName());
//                            field.setValue(value.getValue());
//                        }
//                    }
//                    list.add(field);
//                });
//            }
//        }
//        return list;
//    }

    @Override
    public void batchSaveFieldList(List<FranchiseesApprovalTemplateField> fieldVoList) {
        List<FranchiseesApprovalTemplateFieldValue> valueList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(fieldVoList)) {
            fieldVoList.forEach(vo -> {
                FranchiseesApprovalTemplateField field = this.saveOrUpdateById(vo);
//                // 值的保存
//                if (StringUtils.isNotEmpty(vo.getValue())) {
//                    FranchiseesApprovalTemplateFieldValue value = new FranchiseesApprovalTemplateFieldValue();
//                    value.setTemplateId(field.getTemplateId());
//                    value.setFieldType(field.getFieldType());
//                    value.setFieldId(field.getId());
//                    value.setGroupId(field.getGroupId());
//                    value.setValue(vo.getValue());
//                    value.setSelectId(vo.getSelectId());
//                    valueList.add(value);
//                }
                // 保存选择项
                List<FranchiseesApprovalTemplateFieldSelect> selectList = vo.getFieldSelectList();
                if (CollectionUtil.isNotEmpty(selectList)) {
                    selectList.forEach(select -> {
                        select.setFieldId(field.getId());
                    });
                    franchiseesApprovalTemplateFieldSelectService.batchSaveSectionList(selectList);
                }
            });
//            // 保存值
//            if (CollectionUtil.isNotEmpty(valueList)) {
//                franchiseesApprovalTemplateFieldValueService.batchSaveFieldValueList(valueList, fieldVoList.get(0).getGroupId());
//            }
        }
    }

    @Override
    public FranchiseesApprovalTemplateField saveOrUpdateById(FranchiseesApprovalTemplateField field) {
        if (field != null) {
            if (field.getId() != null) {
                this.baseMapper.updateById(field);
            } else {
                this.baseMapper.insert(field);
            }
            return field;
        }
        return null;
    }

    @Override
    public void insert(List<FranchiseesApprovalTemplateSection> sectionList) {

    }
}
